"""
!/usr/bin/env python
-*- coding: utf-8 -*-
@Author  : MarkJia
@Site    : 
@File    : 24、向指定邮箱发送测试报告.py
"""

import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 定义登录信息：
sender = 'MackJia'
receiver = 'leader'
smtpserver = 'https://mail.qq.com/'
username = '214829112@qq.com'
password = 'jpb@214829112'

# 邮件主题
mail_title = '主题：测试报告'

# 读取 html 文件内容
f = open('../unittest框架/report/report.html', 'rb')
mail_body = f.read()
f.close()

# 邮件内容，格式，编码
message = MIMEText(mail_body, 'html', 'utf-8')
# 来源
message['Form'] = sender
# 去向
message['To'] = receiver
# 主题
message['Subject'] = Header(mail_title, 'utf-8')

try:
    smtp = smtplib.SMTP()
    # 连接
    smtp.connect(smtpserver)
    # 登录
    smtp.login(username, password)
    # 发送邮件
    smtp.sendmail(sender, receiver, message.as_string())
    print('发送邮件成功')
    # 退出
    smtp.quit()

except smtplib.SMTPException:
    print('发送邮件失败')
